/************************************************
* @author:liyou
* @version: 1.0
* @description:
************************************************/

#include <iostream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <string>
#include <cstring>
#include <fstream>

#include <unistd.h>

#include "init_project/init.h"
using namespace std;

int main(int argc, char** argv)
{
    /*
    FILE *fp = nullptr;
    char buf[100]={0};
    char serial[] = "1422622060246";
    fp = popen("less /sys/firmware/devicetree/base/serial-number", "r");
    if(fp) {

        int ret = fread(buf,1,sizeof(buf)-1,fp);
        if(ret > 0) {
            std::cout << buf << std::endl;
        }
        pclose(fp);
    }

    std::cout <<strcmp(buf, serial) << std::endl;

    if(strcmp(buf, serial))
    {
        std::cout << "success!!!" << std::endl;
    }

//    char *cwd = get_current_dir_name();
//    string root_path = cwd;
//    free(cwd);
//    cout << "Current working directory: " << root_path << endl;

*/
    string root_path = argv[1];

    Init init;
    cout << init.name_feature << endl;
    cout << init.name_track << endl;
    cout << init.name_head << endl;


    string fea_name_onnx =  root_path+"/"+"temp1.onnx";
    string fea_name_engine = root_path+"/"+"temp1";
    string fea = init.com_string(fea_name_onnx, fea_name_engine);

    cout << fea << endl;


    string track_name_onnx =  root_path+"/"+"temp2.onnx";
    string track_name_engine = root_path+"/"+"temp2";
    string track = init.com_string(track_name_onnx, track_name_engine);
    cout << track << endl;


    string head_name_onnx =  root_path+"/"+"temp3.onnx";
    string head_name_engine = root_path+"/"+"temp3";
    string head = init.com_string(head_name_onnx, head_name_engine);
    cout << head << endl;

    //system("/usr/src/tensorrt/bin/trtexec --onnx=/tmp/tmp.1JWi4FHBGP/temp1.onnx --workspace=4096
    // --saveEngine=/tmp/tmp.1JWi4FHBGP/cmake-build-debug-202/WRBqHdrOkyIDsdRMwRSYLBfaCYEdgxPl --fp16");

    //转换engine
    system(fea.c_str());
    system(track.c_str());
    system(head.c_str());


    //加密
    // 13位需填充到32位
    string pw = "lyzklx";
    string key = "1422622060246" + pw + "1422622060246";
    cout << key << endl;

    init.encrypt_file(fea_name_engine, root_path+"/"+init.name_feature, key);
    init.encrypt_file(track_name_engine, root_path+"/"+init.name_track, key);
    init.encrypt_file(head_name_engine, root_path+"/"+init.name_head, key);


    // delete source
    system("rm -rf /home/lx/temp/config/temp*");
    cout << "delete success!" << endl;


    // 对加密文件生成干扰文件
    cout << "**************************" << endl;
    //    int seq = 10001 % (20+1);
    //    int len = 9999 / 20;
    //cout << (root_path+"/"+init.name_feature) << endl;


    int n = 20;
    //fea
    string temp_fea = root_path+"/"+init.name_feature;
    std::ifstream file(temp_fea, std::ios::binary);
    if (!file.good()) {
        std::cerr << "read " << temp_fea << " error!" << std::endl;
        return -1;
    }
    file.seekg(0, file.end);
    int size = file.tellg();
    file.seekg(0, file.beg);
    file.close();


    for(int i=0; i<n; ++i)
    {
        string file_name = init.genger_string(32);
        file_name = (root_path+"/") + file_name;
        cout << file_name << endl;
        cout << i << endl;
        init.disturb_file(temp_fea, file_name, n, i);
    }

    //track
    string temp_track = root_path+"/"+init.name_track;
    std::ifstream track_file(temp_track, std::ios::binary);
    if (!track_file.good()) {
        std::cerr << "read " << temp_track << " error!" << std::endl;
        return -1;
    }
    track_file.seekg(0, track_file.end);
    size = track_file.tellg();
    track_file.seekg(0, track_file.beg);
    track_file.close();

    for(int i=0; i<n; ++i)
    {
        string file_name = init.genger_string(32);
        file_name = (root_path+"/") + file_name;
        cout << file_name << endl;
        cout << i << endl;
        init.disturb_file(temp_track, file_name, n, i);
    }

    //head
    string temp_head = root_path+"/"+init.name_head;
    std::ifstream head_file(temp_head, std::ios::binary);
    if (!head_file.good()) {
        std::cerr << "read " << temp_head << " error!" << std::endl;
        return -1;
    }
    head_file.seekg(0, head_file.end);
    size = head_file.tellg();
    head_file.seekg(0, head_file.beg);
    head_file.close();

    for(int i=0; i<n; ++i)
    {
        string file_name = init.genger_string(32);
        file_name = (root_path+"/") + file_name;
        cout << file_name << endl;
        cout << i << endl;
        init.disturb_file(temp_head, file_name, n, i);
    }

    return 0;
}
